package com.xiguthings.waka.web.controller.cdcard;

import javax.annotation.Resource;

import com.xiguthings.waka.dao.DataBaseStatus;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.CoolDownCard;
import com.xiguthings.waka.po.CoolDownCardExample;
import com.xiguthings.waka.po.PaperCard;
import com.xiguthings.waka.po.PaperCardExample;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.web.controller.papercard.PaperCardVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.service.cdcard.ICoolDownCardService;
import com.xiguthings.waka.web.controller.base.AbstractController;
import com.xiguthings.waka.web.controller.base.ResponseEntity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/cdcard")
@Api(tags = "冷却加速卡")
public class CDcardController extends AbstractController {
    @Resource
    private ICoolDownCardService cdCardService;


    /**
     * 创建查询条件
     *
     * @param cDcardVo
     * @param example
     * @throws NumberFormatException
     */
    private void queryConditions(CDcardVo cDcardVo, CoolDownCardExample example) throws NumberFormatException {

        CoolDownCardExample.Criteria criteria = example.createCriteria();
        // 如果查询条件为空的
        if (cDcardVo == null) {
            // 则查询所有未被删除的
            criteria.andStatusNotEqualTo(DataBaseStatus.DELETE);
            return;
        }

        // 如果有查询条件
        CoolDownCard coolDownCard = cDcardVo.getCdCard();
        // 添加查询条件
        if (coolDownCard != null) {

            if (StringUtils.isNotBlank(coolDownCard.getCoolDownCardId())) {
                criteria.andCoolDownCardIdEqualTo(coolDownCard.getCoolDownCardId());
            }
            if (coolDownCard.getHotValue() != null) {
                criteria.andHotValueEqualTo(coolDownCard.getHotValue());
            }
            if (StringUtils.isNotBlank(coolDownCard.getDescr())) {
                criteria.andDescrLike(("%" + coolDownCard.getDescr() + "%"));
            }


        }

        if (StringUtils.isNotBlank(cDcardVo.getInStatus())) {
            // 添加包含的状态
            String statuses = cDcardVo.getInStatus();
            String[] statusArr = statuses.split(",");
            List<Integer> statusList = new ArrayList<>();
            for (String str : statusArr) {
                statusList.add(new Integer(str));
            }
            criteria.andStatusIn(statusList);
        } else {
            // 如果状态的查询条件是空的，则查询没有被删除的
            criteria.andStatusNotEqualTo(DataBaseStatus.DELETE);
        }


    }


    /**
     * 修改或者添加
     *
     * @param cdCardVo
     * @return
     * @throws Exception
     */
    @ApiOperation(tags = {
            "1.控制台"}, value = "修改/添加", httpMethod = "POST", notes = "修改时，必须填写“主键”参数。<br>添加新记录时，hotValue是必填的", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cdCard.coolDownCardId", value = "主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "cdCard.hotValue", value = "加速时间", dataType = "long", required = false, paramType = "query"),
            @ApiImplicitParam(name = "cdCard.descr", value = "描述", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "cdCard.status", value = "状态：0--正常，99--删除", dataType = "int", required = false, paramType = "query")})
    @RequestMapping("/save")
    @ResponseBody
    public ResponseEntity save(@ApiIgnore CDcardVo cdCardVo) throws Exception {
        if (cdCardVo == null || cdCardVo.getCdCard() == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "无法操作空数据");
        }

        CoolDownCard cdCard = cdCardVo.getCdCard();
        String id = cdCard.getCoolDownCardId();
        String msg = null;
        if (StringUtils.isNotBlank(id)) {
            // 修改
            int row = cdCardService.updateByPrimaryKeySelective(cdCard);
            if (row > 0) {
                msg = "修改成功";
                return ajaxReturnMap(CODE_SUCCESS, msg, cdCard.getCoolDownCardId());
            } else {
                msg = "修改失败，数据库中可能不存在这条数据";
            }
        } else {
            int row = cdCardService.insert(cdCard);
            if (row > 0) {
                msg = "添加";
                return ajaxReturnMap(CODE_SUCCESS, msg, cdCard.getCoolDownCardId());
            } else {
                msg = "添加失败，请重试";
            }
        }
        return ajaxReturnMap(CODE_FAULT, msg, cdCard.getCoolDownCardId());
    }

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "根据主键获取记录详情", httpMethod = "GET", notes = "coolDownCardId是必填", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cdCard.coolDownCardId", value = "主键id", dataType = "String", required = true, paramType = "query")
    })
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseEntity detail(@ApiIgnore CDcardVo cdCardVo) throws Exception {
        if (cdCardVo == null
                || cdCardVo.getCdCard() == null
                || StringUtils.isBlank(cdCardVo.getCdCard().getCoolDownCardId())) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "coolDownCardId必填");
        }
        CoolDownCard cdCard = cdCardService.selectByPrimaryKey(cdCardVo.getCdCard().getCoolDownCardId());

        return ajaxReturnMap(CODE_SUCCESS, null, cdCard);
    }

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "删除", httpMethod = "GET", notes = "coolDownCardId必填", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cdCard.coolDownCardId", value = "主键id", dataType = "String", required = true, paramType = "query")})
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseEntity delete(@ApiIgnore CDcardVo cdCardVo) throws Exception {
        if (cdCardVo == null
                || cdCardVo.getCdCard() == null
                || StringUtils.isBlank(cdCardVo.getCdCard().getCoolDownCardId())) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, "coolDownCardId必填");
        }
        int row = cdCardService.deleteByPrimaryKey(cdCardVo.getCdCard().getCoolDownCardId());
        String msg = null;
        if (row > 0) {
            msg = "删除成功";
        } else {
            msg = "这条数据在此之前可能已经被删除了";
        }
        return ajaxReturnMap(CODE_SUCCESS, msg, null);
    }

    /**
     * 分页查询
     *
     * @param cdCardVo
     * @param page
     * @return
     * @throws Exception
     */

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "分页查询", httpMethod = "GET", notes = "可根据条件进行筛选", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cdCard.coolDownCardId", value = "主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "cdCard.hotValue", value = "加速时间", dataType = "long", required = false, paramType = "query"),
            @ApiImplicitParam(name = "cdCard.descr", value = "描述", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "inStatus", value = "状态：0--正常，99--删除", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码，如果没有值，则使用默认值。开始值从 1 开始计算", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "每页的数量,如果没有值，则使用默认值", dataType = "int", required = false, paramType = "query")})
    @RequestMapping("/pagelist")
    @ResponseBody
    public ResponseEntity pageList(@ApiIgnore CDcardVo cdCardVo, @ApiIgnore Page page) throws Exception {
        CoolDownCardExample example = new CoolDownCardExample();
        queryConditions(cdCardVo, example);
        super.selectByPage(page, example, cdCardService);
        return ajaxReturnMap(CODE_SUCCESS, null, page);
    }
}
